<template>
  <div class="wrapper">
    <img
      class="wrapper__img"
      src="http://www.dell-lee.com/imgs/vue3/user.png"
      alt=""
    />
    <div class="wrapper__input">
      <input
        type="text"
        class="wrapper__input__content"
        placeholder="请输入用户名"
        v-model="username"
      />
    </div>
    <div class="wrapper__input">
      <input
        type="password"
        class="wrapper__input__content"
        placeholder="请输入密码"
        v-model="password"
      />
    </div>
    <div class="wrapper__login-button" @click="handleLogin">登录</div>
    <div class="wrapper__login-link" @click="handleRegister">立即注册</div>
    <Toast v-if="show" :message="toastMessage" />
  </div>
</template>

<script>
import { reactive, toRefs } from "@vue/reactivity";
import { useRouter } from "vue-router";
// import axios from "axios";
import { post } from "../../utils/request";
import Toast, { useToastEffect } from "../../components/Toast.vue";

//修改Content-Type
// axios.defaults.headers.post["Content-Type"] = "application/json";

//弹窗有关的内容封装在这里
/* const useToastEffect = () => {
  const toastData = reactive({
    showToast: false,
    toastMessage: "",
  });
  const showToast = (message) => {
    toastData.showToast = true;
    toastData.toastMessage = message;
    setTimeout(() => {
      toastData.showToast = false;
      toastData.toastMessage = "";
    }, 2000);
  };
  return { toastData, showToast };
}; */

//关于Login的内容封装在这里
const useLoginEffect = (showToast) => {
  const router = useRouter();
  const data = reactive({ username: "", password: "" });
  //引入封装的post的写法
  const handleLogin = async () => {
    try {
      const { username, password } = data;
      if (username === "" || password === "") {
        showToast("用户名或密码错误");
      }
      const result = await post("/api/user/login", {
        username: data.username,
        password: data.password,
      });
      if (result.data.errno === 0) {
        localStorage.isLogin = true;
        router.push({ name: "Home" });
      } else {
        showToast("登录失败");
      }
    } catch (e) {
      showToast("请求失败");
    }
  };
  const { username, password } = toRefs();
  return { username, password, handleLogin };
};

//关于Register的内容封装在这里
const useRegisterEffect = () => {
  const router = useRouter();
  const handleRegister = () => {
    router.push({ name: "Register" });
  };
  return { handleRegister };
};

export default {
  name: "Login",
  components: { Toast },
  setup() {
    const { show, toastMessage, showToast } = useToastEffect();
    const { username, password, handleLogin } = useLoginEffect(showToast);
    const { handleRegister } = useRegisterEffect();

    /* const handleLogin = () => {
      axios
        .post(
          "https://www.fastmock.site/mock/ae8e9031947a302fed5f92425995aa19/jd/api/user/login",
          {
            username: data.username,
            password: data.password,
          }
        )
        .then(() => {
          localStorage.isLogin = true;
          router.push({ name: "Home" });
        })
        .catch(() => {
          alert("登录失败");
        });
    }; */

    //使用 async await 语法写法
    /*  const handleLogin = async () => {
      try {
        const result = await axios.post(
          "https://www.fastmock.site/mock/ae8e9031947a302fed5f92425995aa19/jd/api/user/login",
          {
          username: data.username,
          password: data.password,
          }
        );
        if (result.data.errno === 0) {
          localStorage.isLogin = true;
          router.push({ name: "Home" });
        } else {
          alert("登录失败");
        }
      } catch (e) {
        alert("请求失败");
      }
    }; */

    return {
      handleLogin,
      handleRegister,
      username,
      password,
      show,
      toastMessage,
    };
  },
};
</script>

<style lang="scss" scoped>
@import "../../style/viriables.scss";
.wrapper {
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  transform: translateY(-50%);
  &__img {
    display: block;
    margin: 0 auto 0.4rem auto;
    width: 0.66rem;
    height: 0.66rem;
  }
  &__input {
    margin: 0 0.4rem 0.16rem 0.4rem;
    padding: 0 0.16rem;
    height: 0.48rem;
    background: #f9f9f9;
    border: 1px solid rgba(0, 0, 0, 0.1);
    border-radius: 6px;
    &__content {
      border: none;
      outline: none;
      width: 100%;
      line-height: 0.48rem;
      background: none;
      font-size: 0.16rem;
      color: $content-notice-fontcolor;
      &::placeholder {
        color: $content-notice-fontcolor;
      }
    }
  }
  &__login-button {
    margin: 0.32rem 0.4rem 0.16rem 0.4rem;
    line-height: 0.48rem;
    font-size: 0.16rem;
    text-align: center;
    background: #0091ff;
    box-shadow: 0 0.04rem 0.08rem 0;
    border-radius: 0.04rem;
    color: $bgColor;
  }
  &__login-link {
    font-size: 0.14rem;
    color: $content-notice-fontcolor;
    text-align: center;
  }
}
</style>